package jmvm.schemes;

import java.awt.Point;
import java.util.ArrayList;

import jmvm.components.*;
import jmvm.gui.GuiLabel;
import jmvm.gui.GuiLine;
import jmvm.gui.GuiScheme;



public class Addr {
	
	public static IntToInt IBUS_4_0;
	
	public static REG GPRAR, SP, CW, CW1;
	public static ADD ADDadr;
	public static GPR GPRegs; //OBRATI PAZNJU!!!!!!!!!!!!!!!!!!!!!!!!!
	///////////////////////////////////public static GPR2 GPRegs;
	
	public static TSB GPRout, SPout, ADDout;
	
	////////////
	//////////// Grafika
	////////////
	
	public static GuiScheme gui;

	public static GuiScheme getGui() {
		return gui;
	}
	
	public static void gui() {

		GuiLine line; // Pomocna promenljiva
		gui = new GuiScheme("jmvm/jmvm/images/addr.png");
		
		ArrayList<ArrayList<Point>> sections;
		ArrayList<Point> points;

		// 
		// 
		// IBUS
		// 
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(561,16));
		points.add(new Point(561,763));
		sections.add(points);
		line=new GuiLine(sections,Bus.IBUS());
		gui.addLine(line);
		points=new ArrayList<Point>();
		points.add(new Point(559,16));
		points.add(new Point(559,763));
		sections.add(points);
		line=new GuiLine(sections,Bus.IBUS());
		gui.addLine(line);

		// 
		// 
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(185,65));
		points.add(new Point(185,43));
		points.add(new Point(559,43));
		sections.add(points);
		line=new GuiLine(sections,Bus.IBUS());
		gui.addLine(line);
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(315,44));
		points.add(new Point(315,146));
		sections.add(points);
		line=new GuiLine(sections,Bus.IBUS());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(185,112));
		points.add(new Point(185,193));
		points.add(new Point(224,193));
		sections.add(points);
		line=new GuiLine(sections,GPRAR.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(315,239));
		points.add(new Point(315,272));
		points.add(new Point(394,272));
		sections.add(points);
		line=new GuiLine(sections,GPRegs.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(422,272));
		points.add(new Point(558,272));
		sections.add(points);
		line=new GuiLine(sections,GPRout.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(408,279));
		points.add(new Point(408,286));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.GPRout());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(405,193));
		points.add(new Point(413,193));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.wrGPR());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(87,77));
		points.add(new Point(94,77));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.incGPRAR());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(87,99));
		points.add(new Point(94,99));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.ldGPRAR());
		gui.addLine(line);

		//
		//
		// 
		// 
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(559,328));
		points.add(new Point(292,328));
		points.add(new Point(292,350));
		sections.add(points);
		line=new GuiLine(sections,Bus.IBUS());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(292,398));
		points.add(new Point(292,430));
		points.add(new Point(394,430));
		sections.add(points);
		line=new GuiLine(sections,SP.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(422,431));
		points.add(new Point(559,431));
		sections.add(points);
		line=new GuiLine(sections,SPout.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(292,531));
		points.add(new Point(292,487));
		points.add(new Point(394,487));
		sections.add(points);
		line=new GuiLine(sections,ADDadr.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(422,487));
		points.add(new Point(559,487));
		sections.add(points);
		line=new GuiLine(sections,ADDout.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(559,725));
		points.add(new Point(158,725));
		points.add(new Point(158,692));
		sections.add(points);
		line=new GuiLine(sections,Bus.IBUS());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(426,724));
		points.add(new Point(426,692));
		sections.add(points);
		line=new GuiLine(sections,Bus.IBUS());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(158,644));
		points.add(new Point(158,600));
		points.add(new Point(247,599));
		points.add(new Point(248,579));
		sections.add(points);
		line=new GuiLine(sections,CW.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(426,644));
		points.add(new Point(426,600));
		points.add(new Point(337,600));
		points.add(new Point(337,579));
		sections.add(points);
		line=new GuiLine(sections,CW1.getOut(0));
		gui.addLine(line);

		//
		//
		// 
		// 
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(194,362));
		points.add(new Point(202,362));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.incSP());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(194,385));
		points.add(new Point(202,385));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.decSP());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(383,362));
		points.add(new Point(391,362));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.ldSP());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(408,444));
		points.add(new Point(408,436));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.SPout());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(408,500));
		points.add(new Point(408,493));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.ADDout());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(60,679));
		points.add(new Point(68,679));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.ldCW());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(329,679));
		points.add(new Point(336,679));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.ldCW1());
		gui.addLine(line);

		//
		//

		gui.addLabel(new GuiLabel(430, 40, IBUS_4_0.getOut(0)));
		gui.addLabel(new GuiLabel(168, 180, GPRAR.getOut(0)));
		gui.addLabel(new GuiLabel(345, 290, GPRegs.getOut(0)));
		gui.addLabel(new GuiLabel(534, 173, Bus.IBUS()));
		gui.addLabel(new GuiLabel(320, 448, SP.getOut(0)));
		gui.addLabel(new GuiLabel(310, 480, ADDadr.getOut(0)));
		gui.addLabel(new GuiLabel(147, 597, CW.getOut(0)));
		gui.addLabel(new GuiLabel(430, 600, CW1.getOut(0)));

		
		
	}
	/////////////
	/////////////
	/////////////
	
	public static void init() {
		////
		IBUS_4_0 = new IntToInt(16, 5, false, false);
		////
		
		GPRAR = new REG(1, "GPRAR");
		GPRAR.getOut(0).setIsInt();
		GPRAR.getOut(0).setNumOfLines(5);
		
		SP = new REG(1, "SP");
		SP.getOut(0).setIsInt();
		SP.getOut(0).setNumOfLines(16);
		SP.initVal(4096);//1000h

		CW = new REG(1, "CW");
		CW.getOut(0).setIsInt();
		CW.getOut(0).setNumOfLines(16);
		
		CW1 = new REG(1, "CW1");
		CW1.getOut(0).setIsInt();
		CW1.getOut(0).setNumOfLines(16);
		
		////////////////////////////GPRegs = new GPR2(32);
		GPRegs = new GPR(32);
		GPRegs.getOut(0).setNumOfLines(16);
		
		ADDadr = new ADD();
		ADDadr.getOut(0).setIsInt();
		ADDadr.getOut(0).setNumOfLines(16);
		
		//// TSB GPRout, SPout, ADDout;
		
		GPRout = new TSB();
		GPRout.getOut(0).setNumOfLines(16);
		SPout = new TSB();
		SPout.getOut(0).setNumOfLines(16);
		ADDout = new TSB();
		ADDout.getOut(0).setNumOfLines(16);
		
	}
	
	public static void connect() {
		////
		IBUS_4_0.setInputPin(0, Bus.IBUS());
		////
		
		GPRAR.setInputPin(0, IBUS_4_0.getOut(0));
		GPRAR.setPinLd(UpravOper.ldGPRAR());
		GPRAR.setPinInc(UpravOper.incGPRAR());
		
		SP.setInputPin(0, Bus.IBUS());
		SP.setPinInc(UpravOper.incSP());
		SP.setPinDec(UpravOper.decSP());
		SP.setPinLd(UpravOper.ldSP());

		CW.setInputPin(0, Bus.IBUS());
		CW.setPinLd(UpravOper.ldCW());
		
		CW1.setInputPin(0, Bus.IBUS());
		CW1.setPinLd(UpravOper.ldCW1());

		////////////////////////GPRegs.setPinAddress(GPRAR.getOut(0));
		////////////////////////GPRegs.setInputPin(0, Bus.IBUS());
		////////////////////////GPRegs.setPinWr(UpravOper.wrGPR());
		
		GPRegs.setAdressPin(GPRAR.getOut(0));
		GPRegs.setInputDataPin(Bus.IBUS());
		GPRegs.setWrite(UpravOper.wrGPR());
		GPRegs.setRead(new Pin(true));
		
		ADDadr.setPinA(CW.getOut(0));
		ADDadr.setPinB(CW1.getOut(0));
		
		// TSBovi
		
		GPRout.setInputPin(0, GPRegs.getOut(0));
		GPRout.setCtrl(UpravOper.GPRout());
		Bus.addOnIBUS(GPRout.getOut(0));
		
		SPout.setInputPin(0, SP.getOut(0));
		SPout.setCtrl(UpravOper.SPout());
		Bus.addOnIBUS(SPout.getOut(0));
		
		ADDout.setInputPin(0, ADDadr.getOut(0));
		ADDout.setCtrl(UpravOper.ADDout());
		Bus.addOnIBUS(ADDout.getOut(0));
	
	}
	
	
	//public static REG GPRAR, SP, CW, CW1;
	public static void ispisiRegistre() {
		System.out.println(" Registri:" + " GPRAR=" + GPRAR.getOut(0).getIntVal() + " SP=" + SP.getOut(0).getIntVal() +
							" CW=" + CW.getOut(0).getIntVal() + " CW1=" + CW1.getOut(0).getIntVal() );
	}

	public static GPR GPR() {
		return GPRegs;
	}
}
